﻿CREATE VIEW dbo.v_TruthInLending
AS
SELECT     TIL.LoanID, TIL.DealerID, TIL.DealerSalesPersonID, TIL.StatusID, TIL.Status, TIL.VerificationStatusID, TIL.Status_Verification, TIL.ContractHolderID, TIL.SalesPrice, 
                      TIL.VIN, TIL.BookValue, TIL.BookValueDesc, TIL.ModelAge, TIL.Mileage, TIL.MilesPerYear, TIL.SalesTaxPercent, TIL.Lamp_SalesTaxPercent, 
                      TIL.Lamp_SalesTaxAmount, TIL.DownPayment, TIL.DealerFee, TIL.TaxableFees, TIL.TradeIn, TIL.UnpaidBalance, TIL.GAP, TIL.ServiceContract, TIL.NonTaxableFees, 
                      TIL.TagFee, TIL.APROverride, TIL.LoanTerm, TIL.ContractDate, TIL.FirstPaymentDate, CONVERT(varchar, TIL.ContractDate, 101) AS ContractDate_F, CONVERT(varchar, 
                      TIL.FirstPaymentDate, 101) AS FirstPaymentDate_F, DATEDIFF(dd, TIL.ContractDate, TIL.FirstPaymentDate) AS DaysTo1stPayment, TIL.AddOnRateOverride, 
                      TIL.PaymentOverride, TIL.NumberOfPayments, TIL.IsRule78, TIL.IsSUV, TIL.TrackerTypeID, TIL.SalesTaxAmount, TIL.UnPaidBalanceWithTax, 
                      TIL.Lamp_UnPaidBalanceWithTax, TIL.AmountFinanced, TIL.PaymentByAPR, TIL.APRByPayment, TIL.APR, TIL.Payment, 
                      TIL.NumberOfPayments * TIL.Payment AS TotalPayments, TIL.NumberOfPayments * TIL.Payment - TIL.AmountFinanced AS FinanceCharge, 
                      TIL.NumberOfPayments * TIL.Payment + ISNULL(TIL.DownPayment, 0) + ISNULL(TIL.TradeIn, 0) AS TotalSalesPrice, CONVERT(money, 
                      dbo.RATE_IR(TIL.AmountFinanced, TIL.Payment, TIL.NumberOfPayments, TIL.ContractDate, TIL.FirstPaymentDate, TIL.PaymentsPerYear, TIL.DayCountConventionID) 
                      * 100) AS AddOnRateByPayment, TIL.PaymentOverrideFlag, TIL.APROverrideFlag, dbo.t_PurchaseBatch_Loans.BatchID AS PurchaseBatchID, 
                      dbo.t_PurchaseBatch.Name AS PurchaseBatchName, ISNULL(dbo.t_Collection.StatusID, dbo.fs_StateID__Collection__Start()) AS CollectionStatusID, TIL.IsIndependent, 
                      ISNULL(dbo.t_BuyBack.StatusID, dbo.fs_StateID__BuyBack__InActive()) AS StatusID_BuyBack, TIL.PaymentsPerYear, TIL.InterestType, TIL.PaymentTypeID, 
                      ISNULL(dbo.t_Collection.StatusID, dbo.fs_StateID__Collection__Start()) AS StatusID_Collection, TIL.InterestTypeName, TIL.PaymentTypeCode, 
                      TIL.DayCountConventionID, dbo.ts_DayCountConvention.Name AS DayCountConventionName, TIL.PrincipalBalance, dbo.t_Borrower.FirstName, 
                      dbo.t_Borrower.LastName, dbo.t_Borrower.SSN, TIL.BookValueCurrent, CONVERT(int, ISNULL(TIL.PrincipalBalance, TIL.AmountFinanced) 
                      / ISNULL(NULLIF (TIL.BookValueCurrent, 0), NULLIF (TIL.BookValue, 0)) * 100) AS PLTV, TIL.BookValueCurrentTypeID, TIL.BookValueCurrentDesc, TIL.LoanState, 
                      TIL.CarYear, TIL.GUID_Documents, TIL.StatusID_Repo, TIL.StatusUser_Repo, TIL.StatusDate_Repo, TIL.Status_Repo, CONVERT(numeric(18, 2), 
                      TIL.Payment / NULLIF (dbo.PaymentFreqWeight(TIL.PaymentsPerYear), 0)) AS PaymentPerMonth, TIL.StatusID_BK, TIL.StatusUser_BK, TIL.StatusDate_BK, 
                      ISNULL(usix.t_CustomerFile_Export.[Status Code], 'X') AS [Status Code], TIL.BookValueCurrentDate, TIL.BookValueProviderCode
FROM         dbo.t_Loan_Borrower INNER JOIN
                      dbo.t_Borrower ON dbo.t_Loan_Borrower.BorrowerID = dbo.t_Borrower.ID INNER JOIN
                      dbo.ts_DayCountConvention INNER JOIN
                      dbo.v_TruthInLending_30 AS TIL ON dbo.ts_DayCountConvention.ID = TIL.DayCountConventionID ON dbo.t_Loan_Borrower.LoanID = TIL.LoanID LEFT OUTER JOIN
                      usix.t_CustomerFile_Export ON TIL.LoanID = usix.t_CustomerFile_Export.LoanID LEFT OUTER JOIN
                      dbo.t_BuyBack ON TIL.LoanID = dbo.t_BuyBack.LoanID LEFT OUTER JOIN
                      dbo.t_Collection ON TIL.LoanID = dbo.t_Collection.LoanID LEFT OUTER JOIN
                      dbo.t_PurchaseBatch INNER JOIN
                      dbo.t_PurchaseBatch_Loans ON dbo.t_PurchaseBatch.ID = dbo.t_PurchaseBatch_Loans.BatchID ON TIL.LoanID = dbo.t_PurchaseBatch_Loans.LoanID
WHERE     (dbo.t_Loan_Borrower.Pos = 0)

GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane1', @value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[43] 4[18] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1[33] 4[43] 3) )"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1[46] 4) )"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4) )"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 1
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "t_Loan_Borrower"
            Begin Extent = 
               Top = 229
               Left = 340
               Bottom = 322
               Right = 491
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Borrower"
            Begin Extent = 
               Top = 263
               Left = 577
               Bottom = 371
               Right = 799
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "ts_DayCountConvention"
            Begin Extent = 
               Top = 383
               Left = 372
               Bottom = 476
               Right = 523
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "TIL"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 292
               Right = 228
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_CustomerFile_Export (usix)"
            Begin Extent = 
               Top = 530
               Left = 536
               Bottom = 638
               Right = 735
            End
            DisplayFlags = 280
            TopColumn = 31
         End
         Begin Table = "t_BuyBack"
            Begin Extent = 
               Top = 127
               Left = 624
               Bottom = 235
               Right = 779
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Collection"
            Begin Extent = 
               Top = 81
               Left = 391
               Bottom = 222
               Right =', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_TruthInLending';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane2', @value = N'542
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_PurchaseBatch"
            Begin Extent = 
               Top = 2
               Left = 590
               Bottom = 111
               Right = 798
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_PurchaseBatch_Loans"
            Begin Extent = 
               Top = 8
               Left = 341
               Bottom = 87
               Right = 521
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
      PaneHidden = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 68
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 6255
         Alias = 1890
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_TruthInLending';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPaneCount', @value = 2, @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_TruthInLending';

